package com.kakaxi.mybilibili.module.home.live.view;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.bean.LiveRoomBean;
import com.kakaxi.mybilibili.module.home.live.presenter.ILiveRoomPresenter;
import com.kakaxi.mybilibili.module.home.live.presenter.LiveRoomPresenter;
import com.kakaxi.mybilibili.view.MyJCVideoPlayer;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * 直播间活动类
 * Created by kakaxi on 16/10/31.
 */

public class LiveRoomActivity extends NavigationActivity implements ILiveRoomView, View
        .OnTouchListener {

    /** 活动传递参数直播间id **/
    public static final String EXTRA_ROOM_ID = "roomId";

    /** 活动传递参数播放链接 **/
    public static final String EXTRA_PLAY_URL = "playUrl";

    /** 活动传递参数主播名字 **/
    public static final String EXTRA_OWN_NAME = "ownName";

    /** 最外层布局 **/
//    @BindView(R.id.drawlayout_live_room_main)
//    DrawerLayout mMainDrawLayout;

//    @BindView(R.id.toolbar_live_room)
//    Toolbar mToolbar;

    /** 观众标签布局 **/
    @BindView(R.id.tablayout_live_room_tab)
    TabLayout mWatcherTabLayout;

    /** 观众滑动视图 **/
    @BindView(R.id.vp_live_room_pager)
    ViewPager mWatcherPager;

    /** 标题文本控件 **/
    @BindView(R.id.txt_live_room_title)
    TextView mTitleTxt;

    /** 主播昵称文本控件 **/
    @BindView(R.id.txt_live_room_own_name)
    TextView mOwnNameTxt;

    /** 观看人数文本控件 **/
    @BindView(R.id.txt_live_room_watching)
    TextView mWatchingTxt;

    /** 主播等级文本控件 **/
    @BindView(R.id.txt_live_room_own_level)
    TextView mOwnLevelTxt;

    /** 关注人数文本控件 **/
    @BindView(R.id.txt_live_room_concern)
    TextView mConcernTxt;

    /** 主播头像 **/
    @BindView(R.id.civ_live_room_own_icon)
    CircleImageView mOwnIconCircleImg;

    /** 输入弹幕输入框 **/
    @BindView(R.id.edt_live_room_input)
    EditText mInputEdt;

    /** 直播播放控**/
    @BindView(R.id.jcvplayer_live_room_player)
    MyJCVideoPlayer mLiveJCVPlayer;

    /** 观众标签布局标题 **/
    private String[] mTabTitleArray = new String[]{
            "互动", "七日榜", "粉丝榜"
    };

    /** 直播间控制层 **/
    private ILiveRoomPresenter mLiveRoomPresenter;

    /** 直播间id **/
    private int mRoomId;

    /** 直播链接 **/
    private String mPlayUrl;

    private String mOwnName;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_live_room;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        // 设置点击屏幕关闭软键盘
        getDrawerLayout().setOnTouchListener(this);
        // 初始化工具栏
//        setSupportActionBar(mToolbar);
//        ActionBar actionBar = getSupportActionBar();
//        if (actionBar != null) {
//            actionBar.setDisplayHomeAsUpEnabled(true);
//        }
        // 初始化观众视图控件
        for (String title : mTabTitleArray) {
            mWatcherTabLayout.addTab(mWatcherTabLayout.newTab().setText(title));
        }
        mWatcherTabLayout.setupWithViewPager(mWatcherPager);
        mWatcherPager.setAdapter(new WatcherFragmentPagerAdapter(getSupportFragmentManager()));
        mWatcherPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastHandler("aaa");
            }
        });
        // 初始化直播控件, 把进度条以及播放时间隐藏
        mLiveJCVPlayer.progressBar.setVisibility(View.INVISIBLE);
        mLiveJCVPlayer.currentTimeTextView.setVisibility(View.INVISIBLE);
        mLiveJCVPlayer.totalTimeTextView.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initData() {
        // 获取上一个活动传过来的数据
        Bundle bundle = getIntent().getExtras();
        mRoomId = bundle.getInt(EXTRA_ROOM_ID);
        mPlayUrl = bundle.getString(EXTRA_PLAY_URL);
        mOwnName = bundle.getString(EXTRA_OWN_NAME);
        if (mRoomId != 0 && mPlayUrl != null && mOwnName != null) {
            // 播放直播链接
            mLiveJCVPlayer.setUp(mPlayUrl, JCVideoPlayer.SCREEN_LAYOUT_LIST, mOwnName + "的直播间");
            mLiveJCVPlayer.startPlayLogic();
            // 初始化直播间数据
            mLiveRoomPresenter = new LiveRoomPresenter(this);
            mLiveRoomPresenter.initLiveRoomData(mRoomId);
        } else {
            toastHandler("出错,原因:缺少必要参数");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mLiveJCVPlayer.release();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // 获取焦点间接取消了输入框的焦点
        v.requestFocus();
        // 关闭软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context
                .INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        return false;
    }

    @Override
    public void showLiveRoomData(LiveRoomBean liveRoomData) {
        mTitleTxt.setText(liveRoomData.getTitle() + " >");
        mOwnNameTxt.setText(liveRoomData.getUname());
        mWatchingTxt.setText("观看: " + liveRoomData.getOnline());
        mOwnLevelTxt.setText("UP" + liveRoomData.getMasterLevel());
        //  mConcernTxt.setText(liveRoomData.getAttention());
        Glide.with(getBaseContext()).load(liveRoomData.getFace()).into(mOwnIconCircleImg);
    }

    /**
     * 观众滑动视图适配器
     */
    private class WatcherFragmentPagerAdapter extends FragmentPagerAdapter {

        public WatcherFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return LiveRoomFragmentFactory.createFragment(position);
        }

        @Override
        public int getCount() {
            return LiveRoomFragmentFactory.FRAGMENT_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabTitleArray[position];
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && getRequestedOrientation() == ActivityInfo
                .SCREEN_ORIENTATION_LANDSCAPE) {
            JCVideoPlayer.backPress();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
